﻿using Microsoft.AspNetCore.Mvc;

using CoreProject.DTO;
using CoreProject.Model;
using AutoMapper;
using Microsoft.Extensions.Caching.Distributed;
using System.Threading.Tasks;
using Microsoft.EntityFrameworkCore.Diagnostics;
using CoreProject.IService;
using Newtonsoft.Json;
// For more information on enabling Web API for empty projects, visit https://go.microsoft.com/fwlink/?LinkID=397860

namespace CoreProject.Api.Controllers
{
    [Route("api/[controller]")]
    [ApiController]
    public class HJQ_store_infoController : ControllerBase
    {
        private readonly ILogger<HJQ_store_infoController> _ise;
        private readonly IDistributedCache _cache;
      

        public IMapper _Imap { get; }
        public IHJQ_store_infoService _Red { get; }

        // GET: api/<HJQ_store_infoController>

        public HJQ_store_infoController(ILogger<HJQ_store_infoController> ise, IMapper Imap, IDistributedCache cache, IHJQ_store_infoService red)
        {
            _ise = ise;
            _Imap = Imap;
            _cache = cache;
            _Red = red;
        
        }
        [HttpGet]
        public async Task<ResultPageList<HJQ_store_infoDto>> Get(int page, int limit, string name = "")
        {
            try
            {
                //获取数据操作
                var dataa = new List<HJQ_store_infoDto>();
                var list = await _Red.GetListAsync(true, a => a.AddressId, b => 1 == 1);
                dataa = _Imap.Map<List<HJQ_store_infoDto>>(list);
                if (!string.IsNullOrWhiteSpace(name))
                {
                    dataa = dataa.Where(a => a.shopName.Contains(name)).ToList();
                }
                var datalist = dataa.OrderBy(a => a.AddressID).Skip((page - 1) * limit).Take(limit).ToList();
                ResultPageList<HJQ_store_infoDto> re = new ResultPageList<HJQ_store_infoDto>();
                re.count = datalist.Count;

                re.msg = datalist.Count > 0 ? "ok" : "error";
                re.code = datalist.Count > 0 ? "200" : "400";
                re.data = datalist;
                return re;
            }
            catch (Exception ex)
            {

                throw;
            }
        }

        // GET api/<HJQ_store_infoController>/5
        [HttpGet("{id}")]
        public string Get(int id)
        {
            return "value";
        }

        // POST api/<HJQ_store_infoController>
        [HttpPost]
        public async Task<ResultHandle> Post(HJQ_store_infoDto value)
        {
            var obj = new HjqStoreInfo()
            {
                Dianpumj = value.dianpumj,
                Email = value.email,
                Fuzer = value.fuzer,
                Jingyingfw = value.jingyingfw,
                KefuPhone = value.kefuPhone,
                Phone = value.phone,
                Liansuo = (bool)value.liansuo,
                YingyeTime = value.yingyeTime,
                ShopName = value.shopName,
                Region = value.region,
                ShopDate = DateOnly.Parse(value.shopDate.ToString("yyyy-MM-dd")),
                WangAddress = value.wangAddress,
                ShopAddress = value.shopAddress,
            };
            var bo = await _Red.AddAsync(obj);
            ResultHandle re = new ResultHandle();
            re.state = bo ? 1 : 0;
            re.code = bo ? "200" : "400";
            re.msg = bo ? "ok" : "error";
            return re;
        }

        // PUT api/<HJQ_store_infoController>/5
        [HttpPut("{id}")]
        public void Put(int id, [FromBody] string value)
        {
        }

        //DELETE api/<HJQ_store_infoController>/5
        /// <summary>
        /// 修改店铺操作
        /// </summary>
        /// <param name="addressid">店铺地址</param>
        /// <returns></returns>
        [HttpDelete()]
        public async Task<ResultHandle> Delete(int addressid)
        {
           
            
          
            var obj  = await _Red.GetAsync(a => a.AddressId == addressid);
            if (obj.Liansuo) 
            {
              obj.Liansuo = false;
            }
            else {
                obj.Liansuo = true;
            }
                var bo = await _Red.UpdateAsync(obj);
            ResultHandle re = new ResultHandle();

            re.code = bo ? "200" : "400";
            re.msg = bo ? "ok" : "error";
            return re;
        }
    }
}
